﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.XmlDataModel;

namespace Tracy.DataModel.UltraExplorer
{
    public abstract class AbstractClassifier<T> : IClassifier<T> where T : XmlDataModelWithAttributesAndID, new()
    {
        public virtual Dictionary<string, List<T>> FindCategories(List<T> items)
        {
            Dictionary<string, List<T>> rtn = new Dictionary<string, List<T>>();
            if (items != null)
            {
                foreach (T item in items)
                {
                    foreach (string key in GetCategories(item))
                    {
                        if (!rtn.Keys.Contains(key))
                        {
                            List<T> matchedItems = new List<T>();
                            matchedItems.Add(item);
                            rtn.Add(key, matchedItems);
                        }
                        else
                        {
                            rtn[key].Add(item);
                        }
                    }
                }
            }
            return rtn;
        }

        protected abstract List<string> GetCategories(T item);

        public virtual string GetCategoryName(string key)
        {
            return key;
        }

        public virtual List<T> GetItems(List<T> items, string key)
        {
            List<T> rtn = new List<T>();
            if (items != null)
            {
                foreach (T item in items)
                {
                    if(MatchCategory(item, key)) rtn.Add(item);
                }
            }
            return rtn;
        }

        public virtual bool MatchCategory(T item, string key)
        {
            foreach (string keyToCompare in GetCategories(item))
            {
                if (key.Equals(keyToCompare, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        public string Parameter
        {
            get;
            set;
        }


    }
}
